package com.food.dao;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.sql.DataSource;

import com.food.dao.in.OrderDAO;
import com.food.model.Order;

public class OrderDAOJdbc implements OrderDAO {
//	private static final String URL = "jdbc:sqlserver://192.168.34.108:1433;database=PJDB";
//	private static final String USER = "sa";
//	private static final String PASSWORD = "sa123456";
	
	private DataSource ds = null;
	public OrderDAOJdbc() {
		try {
			Context ctx = new InitialContext();
			this.ds = (DataSource) ctx.lookup("java:comp/env/jdbc/food");
		} catch (NamingException e) {
			e.printStackTrace();
		}
	}

	
	private static final String INSERT=
			"insert into [order] (mymbid, hismbid) values(?,?)";
	@Override
	public void insertOrder(Order bean) {
		Connection conn=null;
		PreparedStatement stmt=null;
		try {
//			conn = DriverManager.getConnection(URL, USER, PASSWORD);
			conn = this.ds.getConnection();
			stmt = conn.prepareStatement(INSERT);
			stmt.setInt(1, bean.getMymbid());
			stmt.setInt(2, bean.getHismbid());
			stmt.executeUpdate();
		} catch (SQLException e) {
			e.printStackTrace();
		}
		
		finally{
			if (stmt!=null) {
				try {
					stmt.close();
				} catch (SQLException e) {
					e.printStackTrace();
				}
			}
			if (conn!=null) {
				try {
					conn.close();
				} catch (SQLException e) {
					e.printStackTrace();
				}
			}
		}
	}
	private static final String DELECT=
			"delete from [order] where oid =?";
	@Override
	public void deleteOrder(int oid) {
		Connection conn = null;
		PreparedStatement stmt = null;
		try {
//			conn = DriverManager.getConnection(URL, USER, PASSWORD);
			conn = this.ds.getConnection();
			stmt = conn.prepareStatement(DELECT);
			stmt.setInt(1, oid);
			stmt.executeUpdate();
		} catch (SQLException e) {
			e.printStackTrace();
		}
		
		finally{
		if (stmt!=null) {
			try {
				stmt.close();
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
		if (conn!=null) {
			try {
				conn.close();
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
	  }
	}
	private static final String SELECT=
			"select oid ,mymbid, hismbid from [order] where mymbid =?";
	@Override
	public List<Order> findOrderByMb(int mymbid) {
		List<Order> result = null;
		Order order = null;
		Connection conn = null;
		PreparedStatement stmt = null;
		ResultSet rest = null;
		try {
//			conn = DriverManager.getConnection(URL, USER, PASSWORD);
			conn = this.ds.getConnection();
			stmt = conn.prepareStatement(SELECT);
			stmt.setInt(1, mymbid);
			rest = stmt.executeQuery();
			result=new ArrayList<Order>();
			while(rest.next()){
				order = new Order();
				order.setOid(rest.getInt("oid"));
				order.setMymbid(rest.getInt("mymbid"));
				order.setHismbid(rest.getInt("hismbid"));
				result.add(order);
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}
		finally{
		if (rest!=null) {
			try {
				rest.close();
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
		if (stmt!=null) {
			try {
				stmt.close();
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
		if (conn!=null) {
			try {
				conn.close();
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
	}
		return result;
	}
	public static void main(String args[]){
//		OrderDAOJdbc la = new OrderDAOJdbc();
//		Order gr = new Order();
//		gr.setHismbid(2);
//		gr.setMymbid(2);
//		la.insertOrder(gr);
		
		
//		List<Order> l = new ArrayList<Order>();
//		l = la.findOrderByMb(2);
//		System.out.println(l);
		
//		la.deleteOrder(2);
	}

}
